﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class CustomerProfile : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
           /* if (Session[Utils.AppConstants.UserCode] == null
             || Session[Utils.AppConstants.CompanyName] == null
             || Session[Utils.AppConstants.CompanyCode] == null
              || Session[Utils.AppConstants.OutletCode] == null
                || Session[Utils.AppConstants.OutletName] == null
               || Session[Utils.AppConstants.IsSupperUser] == null
              || Session[Utils.AppConstants.Password] == null)
            {
                Response.Redirect("Login.aspx");
            }*/
            try
            {
                /*if (System.Configuration.ConfigurationManager.AppSettings["HoldingDatabase"].ToString() != Session[VSM.Utils.AppConstants.CompanyCode].ToString())
                {
                    Response.Redirect("Homepage.aspx");
                }*/
                if (!IsPostBack)
                {
                    //LoadCompany();
                    //if (Request.QueryString["Company"] == null)
                    {
                        string dbType = string.Empty;
                        if (string.Compare("VSM", (string)Session[Utils.AppConstants.ID_Dealer]) == 0)
                            dbType = Utils.AppConstants.VSM;
                        else
                            dbType = Utils.AppConstants.DEALER;

                        LoadGroup(dbType, (string)Session[Utils.AppConstants.ID_Dealer]);
                        LoadDealer(dbType);
                        LoadCustomers(dbType, ddlGroup.SelectedValue);
                    }
                }
            }
            catch (Exception ex)
            {
                string alert = "alert('Error:" + ex.Message.Replace('\'', ' ') + " ');";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", alert, true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkOutlet_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
                Label outletCode = (Label)gr.FindControl("lblItemCode");
                string popup = "OpenOutlet('" + outletCode.Text + "')";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", popup, true);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
                LinkButton lblItemCode = (LinkButton)gr.FindControl("lblItemCode");
                Label lblID = (Label)gr.FindControl("txtDscription");
                MasterData obj = new MasterData();
                string err = "";//obj.DeleteVendorSetup("this.drdOutlet.SelectedValue.ToString()", lblItemCode.Text);
                if (err.Length > 0)
                {
                    this.lblError.Text = err;
                    return;
                }
                else
                {
                    this.lblError.Text = "Operation complete successful!";
                    //LoadOutletSupplier(this.drdOutlet.SelectedValue.ToString());
                }
                this.lblError.Visible = true;
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable tblBP = (DataTable)Session["BusinessPartner"];
            this.grvItem.PageIndex = e.NewPageIndex;
            BindItem(tblBP);
        }

        private void LoadGroup(string dbType, string DealerID)
        {
            MasterData masterData = new MasterData();
            DataSet dsGroup = masterData.GetBPGroup("C", dbType);
            ddlGroup.DataSource = dsGroup.Tables[0];
            ddlGroup.DataValueField = "GroupCode";
            ddlGroup.DataTextField = "GroupName";
            ddlGroup.DataBind();
            if (ddlGroup.Items.Count > 0)
            {
                if (string.Compare("VSM", (string)Session[Utils.AppConstants.ID_Dealer]) == 0)
                {
                    ddlGroup.SelectedIndex = 1;
                }
                else
                {
                    ddlGroup.SelectedIndex = 0;
                    ddlGroup.Enabled = false;
                }
            }
        }

        private void LoadDealer(string dbType)
        {
            MasterData masterData = new MasterData();
            DataSet dsDealer = masterData.GetDealers(dbType);
            ddlDealer.DataSource = dsDealer.Tables[0];
            ddlDealer.DataValueField = "CardCode";
            ddlDealer.DataTextField = "CardName";
            ddlDealer.DataBind();
            if (ddlDealer.Items.Count > 0)
            {
                if (string.Compare("VSM", (string)Session[Utils.AppConstants.ID_Dealer]) == 0)
                {
                    ddlDealer.SelectedIndex = 0;
                }
                else
                {
                    ddlDealer.Items.FindByValue((string)Session[Utils.AppConstants.ID_Dealer]).Selected = true;
                    ddlDealer.Enabled = false;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadCustomers(string dbType,  string GroupCode)
        {
            GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
            DataSet dsCustomers = GF.GetCustomerProfiles(dbType, (string)Session[Utils.AppConstants.ID_Dealer], "( " + GroupCode + " )");
            BindItem(dsCustomers.Tables[0]);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindItem(DataTable tb)
        {
            Session["BusinessPartner"] = tb;
            this.grvItem.DataSource = tb;
            this.grvItem.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DeleteItem(object sender, GridViewDeleteEventArgs e)
        {
            e.Cancel = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            try
            {
                base.OnLoadComplete(e);
                if (this.Request["__EVENTARGUMENT"] != null && this.Request["__EVENTARGUMENT"].ToString() != "")
                {
                    this.lblError.Visible = false;
                }
            }
            catch (Exception ex)
            {
                this.lblError.Visible = true;
                this.lblError.Text = ex.Message;
            }
        }

        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
                Label lblCardCode = (Label)gr.FindControl("lblCardCode");
                if(ddlGroup.SelectedValue.CompareTo("103") == 0)
                    Response.Redirect("CustomerProfileEdit.aspx?id=" + lblCardCode.Text);
                else if(ddlGroup.SelectedValue.CompareTo("104") == 0)
                    Response.Redirect("DealerProfileEdit.aspx?id=" + lblCardCode.Text);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNewBP_Click(object sender, EventArgs e)
        {
            if (ddlGroup.SelectedValue.CompareTo("103") == 0)
                Response.Redirect("CustomerProfileEdit.aspx?id=");
            else if (ddlGroup.SelectedValue.CompareTo("104") == 0)
                Response.Redirect("DealerProfileEdit.aspx?id=");
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCustomers(Utils.AppConstants.VSM, ddlGroup.SelectedValue);
        }

        protected void ddlDealer_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}